<template>
  <div>

    <el-input v-model="query.name" placeholder="请输入姓名" style="width: 200px"></el-input>
    <el-input v-model="query.phone" placeholder="请输入电话" style="width: 200px"></el-input>
    <el-input v-model="query.city" placeholder="请输入城市" style="width: 200px"></el-input>
    <el-button type="primary" @click="getList()">查询</el-button>
    <el-button type="primary" @click="handleCreate()">新增</el-button>
    
    <el-table
        :data="tableData"
        style="width: 100%">
      <el-table-column
          prop="name"
          label="姓名"
          width="180">
      </el-table-column>
      <el-table-column
          prop="age"
          label="年龄">
      </el-table-column>
      <el-table-column
          prop="sex"
          label="性别">
      </el-table-column>
      <el-table-column
          prop="phone"
          label="电话">
      </el-table-column>
      <el-table-column
          prop="city"
          label="城市">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              @click="handleEdit(scope.row)">修改</el-button>
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.query.page"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="this.query.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total">
    </el-pagination>

    <el-dialog title="学生信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="form.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input v-model="form.sex" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="城市">
          <el-input v-model="form.city" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "CrudView",
  data() {
    return {
      tableData: [],
      total: 0,
      query: {
        page: 1,
        size: 5,
        name: '',
        phone: '',
        city: ''
      },
      dialogFormVisible: false,
      form: {
        id: '',
        name: '',
        age: '',
        sex: '',
        phone: '',
        city: ''
      },
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取列表的查询方法
    getList() {
      this.axios.get('http://localhost:8888/user/getList',{
        params:{
          page: this.query.page,
          size: this.query.size,
          name: this.query.name,
          phone: this.query.phone,
          city: this.query.city,
        }
      }).then((resp) => {
        console.log(resp);
        this.tableData = resp.data.content.list;
        this.total = resp.data.content.total;
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.query.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.query.page = val;
      this.getList();
    },
    handleDelete(scope) {
      let id = scope.row.id
      this.axios.delete('http://localhost:8888/user/delete/' + id).then((resp) => {
        let data = resp.data
        if(data.success) {
          this.getList()
          this.$message({
            message: '已删除成功',
            type: 'success'
          })
        }
      })
    },
    // 新增方法
    handleCreate() {
      this.form = {}
      this.dialogFormVisible = true
    },
    // 修改方法
    handleEdit(row) {
      this.form = row
      this.dialogFormVisible = true
    },
    // 新增和修改的提交表单方法
    submitForm() {
      this.axios.post('http://localhost:8888/user/save/',this.form).then((resp) => {
        let data = resp.data
        if(data.success) {
          this.dialogFormVisible = false
          this.form = {}
          this.getList()  // 完成操作再查询表格
          this.$message({
            message: '已操作成功',
            type: 'success'
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>